package com.ruicar.afs.cloud.account.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.account.common.entity.VoucherInfo;
import com.ruicar.afs.cloud.account.common.service.VoucherEntryService;
import com.ruicar.afs.cloud.account.common.service.VoucherInfoService;
import com.ruicar.afs.cloud.account.vo.VoucherTemplateInfoVo;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author infinity
 * @version 1.0
 * @date 2020/6/22 15:01
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/voucherTemplate")
@Api(value = "voucherTemplate", description = "凭证模板信息")
public class VoucherTemplateInfoController {
    public final VoucherInfoService voucherInfoService;
    public final VoucherEntryService voucherEntryService;

    @PostMapping("/getVoucherTemplateList")
    @ApiOperation(value = "获取凭证模板信息")
    public IResponse<Object> getVoucherTemplateList(@RequestBody QueryCondition<VoucherTemplateInfoVo> condition) {
        IPage iPage = voucherInfoService.page(new Page<>(condition.getPageNumber(), condition.getPageSize()),
                Wrappers.<VoucherInfo>lambdaQuery()
                        .like(StringUtils.isNotEmpty(condition.getCondition().getMainBusiness()), VoucherInfo::getMainBusiness,
                                condition.getCondition().getMainBusiness() != null ? condition.getCondition().getMainBusiness().trim() : condition.getCondition().getMainBusiness())
                        .like(StringUtils.isNotEmpty(condition.getCondition().getChildBusiness()), VoucherInfo::getChildBusiness,
                                condition.getCondition().getChildBusiness() != null ? condition.getCondition().getChildBusiness().trim() : condition.getCondition().getChildBusiness()));
        return IResponse.success(iPage);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存凭证模板信息")
    public IResponse save(@RequestBody @Validated VoucherInfo voucherInfo) {
        return IResponse.success(voucherInfoService.saveOrUpdateVoucherInfo(voucherInfo));
    }

    @PostMapping("/getVoucherInfo/{id}")
    @ApiOperation(value = "通过id获取凭证模板信息")
    public IResponse getVoucherInfo(@PathVariable Long id) {
        return IResponse.success(voucherInfoService.getVoucherInfo(id));
    }

    @PostMapping("/delVoucherInfo/{id}")
    @ApiOperation(value = "通过id删除凭证模板信息")
    public IResponse delVoucherInfo(@PathVariable Long id) {
        return IResponse.success(voucherInfoService.delVoucherInfo(id));
    }

    @PostMapping("/active/{id}")
    @ApiOperation(value = "通过id设置凭证模板信息生效")
    public IResponse active(@PathVariable Long id) {
        return IResponse.success(voucherInfoService.active(id));
    }

    @PostMapping("/invalidity/{id}")
    @ApiOperation(value = "通过id设置凭证模板信息无效")
    public IResponse invalidity(@PathVariable Long id) {
        return IResponse.success(voucherInfoService.invalidity(id));
    }

    @PostMapping("/listVoucher")
    @ApiOperation(value = "查询凭证模板信息")
    @SysLog(value = "查询凭证模板信息")
    public IResponse listVoucher(){
        return IResponse.success(voucherInfoService.listVoucher());
    }
}
